﻿using black_screen_mode_server.Models;
using black_screen_mode_server.Pages;
using black_screen_mode_server.ViewModel;
using DocumentFormat.OpenXml.Office2010.ExcelAc;
//using Irony.Parsing;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;

using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using static black_screen_mode_server.Pages.ListOperationForm;

namespace black_screen_mode_server
{
    /// <summary>
    /// GroupWindow.xaml 的交互逻辑
    /// </summary>
    public partial class GroupWindow : Window
    {
        //private ObservableCollection<ListInfo> listInfos = new ObservableCollection<ListInfo>();

        //public ICommand EditCommand { get; set; }
        //public ICommand DeleteCommand { get; set; }
        GroupWindowViewModel groupWindowViewModel = new GroupWindowViewModel();

        public GroupWindow()
        {
            InitializeComponent();
            //refreh_process();
           // this.listInfoDataGrid.ItemsSource = listInfos;
           this.DataContext = groupWindowViewModel;
         
        }

        private void cmlistSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            groupWindowViewModel.ListType = cmlistSelect.SelectedIndex;
            groupWindowViewModel.refreshPages();
            groupWindowViewModel.ComboxIndex = 0;

           
            
        }

        private void cmPages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(groupWindowViewModel.ComboxIndex >= 0)
            {
                groupWindowViewModel.refresh(groupWindowViewModel.ComboxIndex);
            }
            else
            {
                groupWindowViewModel.refresh(0);
            }
            
        }

        //private void add_click(object sender, RoutedEventArgs e)
        //{
        //    ListOperationForm lstForm = new ListOperationForm(ListOperationForm.operationType.Add,new ListInfo(),cmlistSelect.SelectedIndex);
        //    lstForm.ShowDialog();
        //    refreh_process();

        //}

        //private void refresh_click(object sender, RoutedEventArgs e)
        //{
        //    refreh_process();
        //}

        //private void refreh_process()
        //{
        //    List<ListInfo> infos = ListInfoSqlHelper.GetListInfo(cmlistSelect.SelectedIndex);
        //    listInfos.Clear();
        //    foreach (var item in infos)
        //    {
        //        listInfos.Add(item);
        //    }
        //}

        //private void cmlistSelect_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    refreh_process();
        //}

        //private string connectionString = ProjectTools.configDbConnString;
        //private ObservableCollection<TagItem> lstAllTagItems = new ObservableCollection<TagItem>();
        //private ObservableCollection<TagItem> lstGroupTagItems = new ObservableCollection<TagItem>();

        //public class TagItem
        //{
        //    public string Tag { get; set; }
        //    public bool IsChecked { get; set; }
        //}

        //public GroupWindow()
        //{
        //    InitializeComponent();
        //    lstAllTag.ItemsSource = lstAllTagItems;
        //    lstGroupTag.ItemsSource = lstGroupTagItems;
        //    PopulateComboBox();
        //    UpdateAllCount();
        //    UpdateGroupCount();
        //}

        ////选择分组
        //private void PopulateComboBox()
        //{
        //    try
        //    {
        //        using (SqlConnection connection = new SqlConnection(connectionString))
        //        {
        //            connection.Open();
        //            string query = "select [Name] from [group]";
        //            SqlCommand command = new SqlCommand(query, connection);
        //            SqlDataReader reader = command.ExecuteReader();

        //            while (reader.Read())
        //            {
        //                string groupName = reader["Name"].ToString();
        //                cmbxGroup.Items.Add(groupName);
        //            }

        //            reader.Close();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("错误: " + ex.Message, "提示");
        //    }
        //}


        //public void GroupIdChanged(string groupName)
        //{
        //    try
        //    {
        //        using SqlConnection connection = new SqlConnection(connectionString);
        //        connection.Open();
        //        string query = "select [id] from [group] where name = @Name";
        //        SqlCommand command = new SqlCommand(query, connection);
        //        command.Parameters.AddWithValue("@name", groupName);
        //        SqlDataReader reader = command.ExecuteReader();

        //        while (reader.Read())
        //        {
        //            string groupId = reader["ID"].ToString();
        //            txtGroupId.Text = groupId;
        //        }

        //        reader.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("错误: " + ex.Message, "提示");
        //    }
        //}


        ////切换分组后加载对应List表
        //private void ComboBox_SelectionChanged(object sender, RoutedEventArgs e)
        //{
        //    string selectedGroup = cmbxGroup.SelectedItem.ToString();
        //    LoadlstGroupTag(selectedGroup);
        //    UpdateAllCount();
        //    UpdateGroupCount();
        //    GroupIdChanged(selectedGroup);
        //}


        //public void LoadlstAllTag(string groupName)
        //{
        //    lstAllTagItems.Clear();
        //    try
        //    {
        //        using SqlConnection connection = new SqlConnection(connectionString);
        //        connection.Open();
        //        string query = "SELECT tag FROM black_list WHERE group_id = 0 ";
        //        SqlCommand command = new SqlCommand(query, connection);
        //        command.Parameters.AddWithValue("@Name", groupName);
        //        SqlDataReader reader = command.ExecuteReader();

        //        while (reader.Read())
        //        {
        //            string tag = reader["tag"].ToString();
        //            lstAllTagItems.Add(new TagItem { Tag = tag });
        //        }

        //        reader.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("错误: " + ex.Message, "提示");
        //    }
        //}



        //public void LoadlstGroupTag(string groupName)
        //{
        //    lstGroupTagItems.Clear();
        //    try
        //    {
        //        using SqlConnection connection = new SqlConnection(connectionString);
        //        connection.Open();
        //        string query = "SELECT tag FROM black_list WHERE group_id IN  ( SELECT id FROM [group]  WHERE name = @Name)";
        //        SqlCommand command = new SqlCommand(query, connection);
        //        command.Parameters.AddWithValue("@Name", groupName);
        //        SqlDataReader reader = command.ExecuteReader();

        //        while (reader.Read())
        //        {
        //            string tag = reader["tag"].ToString();
        //            lstGroupTagItems.Add(new TagItem { Tag = tag });
        //        }

        //        reader.Close();
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("错误: " + ex.Message, "提示");
        //    }
        //}


        ////向分组添加点位
        //private void btnAddTag_Click(object sender, RoutedEventArgs e)
        //{
        //    if (cmbxGroup.SelectedItem == null)
        //    {
        //        MessageBox.Show("请选择一个分组。", "提示");
        //        return;
        //    }

        //    var itemsToRemove = lstAllTagItems.Where(x => x.IsChecked).ToList();
        //    foreach (var item in itemsToRemove)
        //    {
        //        lstGroupTagItems.Add(item);
        //        UpdateDatabase(item.Tag, cmbxGroup.SelectedItem.ToString());
        //    }

        //    foreach (var item in itemsToRemove)
        //    {
        //        lstAllTagItems.Remove(item);
        //    }

        //    SortListBoxItems();
        //    UpdateAllCount();
        //    UpdateGroupCount();
        //}

        ////分组内删除点位
        //private void btnRemoveTag_Click(object sender, RoutedEventArgs e)
        //{
        //    if (cmbxGroup.SelectedItem == null)
        //    {
        //        MessageBox.Show("请选择一个分组。", "提示");
        //        return;
        //    }

        //    var itemsToRemove = lstGroupTagItems.Where(x => x.IsChecked).ToList();
        //    foreach (var item in itemsToRemove)
        //    {
        //        lstAllTagItems.Add(item);
        //        UpdateDatabase(item.Tag, "0");
        //    }

        //    foreach (var item in itemsToRemove)
        //    {
        //        lstGroupTagItems.Remove(item);
        //    }

        //    SortListBoxItems();
        //    UpdateAllCount();
        //    UpdateGroupCount();
        //}


        //private void UpdateDatabase(string tag, string groupName)
        //{
        //    try
        //    {
        //        using (SqlConnection connection = new SqlConnection(connectionString))
        //        {
        //            connection.Open();
        //            string query = "UPDATE black_list SET Group_id = ( SELECT id FROM [group]  WHERE name = @Name) WHERE tag = @Tag";
        //            SqlCommand command = new SqlCommand(query, connection);
        //            command.Parameters.AddWithValue("@Name", groupName);
        //            command.Parameters.AddWithValue("@Tag", tag);
        //            command.ExecuteNonQuery();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show("错误: " + ex.Message);
        //    }
        //}

        ////对两个List内的项排序
        //private void SortListBoxItems()
        //{
        //    lstAllTagItems = new ObservableCollection<TagItem>(lstAllTagItems.OrderBy(x => x.Tag));
        //    lstGroupTagItems = new ObservableCollection<TagItem>(lstGroupTagItems.OrderBy(x => x.Tag));
        //    lstAllTag.ItemsSource = lstAllTagItems;
        //    lstGroupTag.ItemsSource = lstGroupTagItems;
        //}


        ////新增分组
        //private void btnAddGoup_Click(object sender, RoutedEventArgs e)
        //{
        //    string newName = Microsoft.VisualBasic.Interaction.InputBox("请输入新的分组名称：", "命名");

        //    // 检查输入是否为空字符串
        //    if (!string.IsNullOrWhiteSpace(newName))
        //    {
        //        using (SqlConnection connection = new SqlConnection(connectionString))
        //        {
        //            connection.Open();
        //            string query = "insert into [group] values (@NewName);";
        //            SqlCommand command = new SqlCommand(query, connection);
        //            command.Parameters.AddWithValue("@NewName", newName);
        //            command.ExecuteNonQuery();
        //        }

        //        ComboBoxItem newItem = new ComboBoxItem();
        //        newItem.Content = newName;
        //        cmbxGroup.Items.Add(newItem);
        //    }
        //}

        ////点位总数
        //private void UpdateAllCount()
        //{
        //    string AllCount = lstAllTag.Items.Count.ToString();

        //    txtAllPointQuantity.Text = AllCount + " 项";
        //}

        ////当前分组点位个数
        //private void UpdateGroupCount()
        //{
        //    string GroupCount = lstGroupTag.Items.Count.ToString();

        //    txtGroupPointQuantity.Text = GroupCount + " 项";
        //}


    }
}
